package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.ResponseResult;
import com.example.domain.entity.Category;
import com.example.domain.entity.ProductCategory;
import com.example.exception.FloraException;
import com.example.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productCategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @GetMapping("/getProductCategoryPage")
    public IPage<ProductCategory> getProductCategoryPage(
            @RequestParam(required = false) String name,
            @RequestParam(required = false,defaultValue = "0") Integer pageNum,
            @RequestParam(required = false,defaultValue = "5") Integer pageSize
    ){
        return productCategoryService.getProductCategoryPage(name,pageNum,pageSize);
    }

    @GetMapping("/getProductCategoryList")
    public ResponseResult getProductCategoryList(){
        return ResponseResult.okResult(productCategoryService.getProductCategoryList());
    }

    @PostMapping("/add")
    public ProductCategory add(@RequestBody ProductCategory productCategory) throws FloraException {
        return productCategoryService.add(productCategory);
    }

    @PutMapping("/update")
    public void update(@RequestBody ProductCategory productCategory,@RequestParam int flag) throws FloraException {
        if(flag==1) {
            productCategoryService.updateCommon(productCategory);
        }
        else{
            productCategoryService.updateStatus(productCategory);
        }
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) throws FloraException{
        productCategoryService.delete(id);
    }


}
